home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / amiga / convrtrs / dgvcon.arc / DGVCON.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-10-18  |  10.9 KB  |  409 lines

  1. /* DgvCon.c
  2.  * 1990/09/20
  3.  * Version 0.02 -- Public Domain
  4.  */
  5.  
  6. /* by Jim Omura, 2A King George's Drive,
  7.  * Toronto, Ontario, M6M 2G9
  8.  * BIX 'jimomura'
  9.  */
  10.  
  11. /* dgvcon [-v?] [-b=nnn] [-c=nnn] [-p=n] [-t=n] [infile] [outfile]
  12.  * Dash Flags:
  13.  *
  14.  *  ?  Short Help
  15.  *  b   Bright +/-255
  16.  *  c   Contrast %
  17.  *  p   bit Planes (7 or 8 -- default is 8)
  18.  *  t   type (1 = 320*200, 2 = 640*400, default = 2)
  19.  *  v   Verbose.
  20.  */
  21.  
  22. /* The '-t' switch is not implimented at this time.
  23.  * The main support code for this switch has been
  24.  * included, but I don't have a test file.
  25.  */
  26.  
  27. /* The actual IFF/ILBM header should be of the type:
  28.  
  29. typedef struct      * BitMapHeader *
  30. {
  31.     USHORT   bm_w,
  32.              bm_h,
  33.              bm_x,
  34.              bm_y;
  35.     UBYTE    bm_nplanes, * UBYTE *
  36.              bm_masking,
  37.              bm_comp,    * Compression *
  38.              bm_pad1;
  39.     USHORT   bm_trans;   * Transparent color *
  40.     UBYTE    bm_xaspect, * UBYTE *
  41.              bm_yaspect;
  42.     short    bm_pgwidth, * USHORT? *
  43.              bm_pgheight;
  44. } IFFBMHDR;
  45.  
  46.  * but the the 68000 processor has problems with Bytewide
  47.  * data.  As such, the Sozobon C creates 16 bit aligned variables
  48.  * which are misaligned if you try to read or write the
  49.  * actual header into the memory structure as declared.
  50.  * In order to avoid this, 'bm_dmwd1' (dummy word 1) style
  51.  * data has been used and separate variables "manually" packed
  52.  * and unpacked where necessary.
  53.  *
  54.  * This module has been ported to the Atari ST via Sozobon C
  55.  * version 1.2.  My version of the compiler has been patched
  56.  * by Ian Lepore of BIX.  The only "unofficial" patch is to the
  57.  * TOP optimizer.  Since the patch is not official yet, it is
  58.  * not likely you will have it unless you have a later version of
  59.  * Sozobon C.  The best advice is to NOT use the TOP optimizer
  60.  * if you are using Sozobon C 1.2 or earlier.
  61.  */
  62.  
  63. #include <stdio.h>
  64. #include <fcntl.h>
  65.  
  66. #include "dgvcon.h"
  67.  
  68. /* ------------------------------------------------------- */
  69.  
  70. /* Global Variables */
  71.  
  72.  
  73. int           bright;           /* Brightness */
  74. int           contrast;
  75. int           inlen;
  76. int           planes;           /* IFF Bit Planes */
  77. int           verbose;
  78. int           white;            /* White value for bright adjust */
  79.  
  80. unsigned char cbyte[1];         /* Conversion Byte */
  81. /* USHORT  conbuf[8];           /* Conversion Buffer */
  82. unsigned char inbuf[DGVBYTES];  /* Input File buffer */
  83. USHORT        outbuf[IFFWMAX];  /* Output File buffer */
  84.  
  85. IFFBMHDR hdrstr;         /* IFFbitmapheader struct */
  86.  
  87. /* ------------------------------------------------------- */
  88.  
  89. main(argc,argv)
  90.  
  91. int argc;
  92. char **argv;
  93.  
  94. {
  95.     static char ifhbmhd[4] =
  96.     {
  97.         'B','M','H','D'
  98.     };
  99.     static char ifhbody[4] =
  100.     {
  101.         'B','O','D','Y'
  102.     };
  103.     static char ifhcmap[4] =
  104.     {
  105.         'C','M','A','P'
  106.     };
  107.     static char ifhform[4] =
  108.     {
  109.         'F','O','R','M'
  110.     };
  111.     static char ifhilbm[4] =
  112.     {
  113.         'I','L','B','M'
  114.     };
  115.  
  116. #ifdef ATARI_ST
  117.     static IFFBMHDR hdrdef =         /* IFFbitmapheader struct */
  118.     {
  119.         640, 400,  /* Width, Height */
  120.         0, 0,      /* X, Y offsets */
  121.         0x0800,    /* 8 Bit Planes, no mask */
  122.         0x0000,    /* Compression == 0, pad byte == 0  */
  123.         0,         /* Transparent colour */
  124.         0x0101,    /* xAspect, yAspect */
  125.         640, 400
  126.     };
  127. #else
  128.     static IFFBMHDR hdrstr =         /* IFFbitmapheader struct */
  129.     {
  130.         640, 400,  /* Width, Height */
  131.         0, 0,      /* X, Y offsets */
  132.         8,         /* Bit Planes */
  133.         0,         /* No Mask == 0 */
  134.         0,         /* Compression == 0 */
  135.         0,         /* Pad byte */
  136.         0,         /* Transparent colour */
  137.         1, 1,      /* xAspect, yAspect */
  138.         640, 400
  139.     };
  140. #endif
  141.  
  142. /* End of Static Variables. */
  143.  
  144.     int             cntr;           /* Local counter */
  145. #ifdef ATARI_ST
  146.     int             errno;          /* OS-9 standard variable for errors */
  147. #endif
  148.     int             fdone;          /* Field done */
  149. /*    long            ifflen;         /* IFF Chunk length */
  150.     long            iflnbmhd;  /* Chunk Lengths */
  151.     long            iflnbody;
  152.     long            iflncmap;
  153.     long            iflnform;
  154.     long            iflnilbm;
  155.  
  156.     int             inpath;         /* Input File pointer */
  157.     int             lastln;         /* Last line */
  158.     int             lncntr;         /* Line Counter */
  159.     int             nclrs;          /* Number of Colour Registers */
  160.     int             newfflag;       /* New Field Flag */
  161.  
  162.     int             outlen;         /* Bytes written for output */
  163.     int             outpath;        /* Output File pointer */
  164.     register int    offset;         /* Input buffer offset */
  165.     char            *strpntr;       /* String Pointer for Write() */
  166.    
  167.     int             report;         /* Function return codes */
  168.  
  169. /* Set Defaults */
  170.  
  171.     bright      = 0;
  172.     contrast    = 100;
  173.     inpath      = STDIN;
  174.     outpath     = STDOUT;
  175.     planes      = 8;
  176.     verbose     = FALSE;
  177.  
  178. hdrstr.bm_w        = hdrdef.bm_w;
  179. hdrstr.bm_h        = hdrdef.bm_h;
  180. hdrstr.bm_x        = hdrdef.bm_x;
  181. hdrstr.bm_y        = hdrdef.bm_y;
  182. hdrstr.bm_dmwd1    = hdrdef.bm_dmwd1;
  183. hdrstr.bm_dmwd2    = hdrdef.bm_dmwd2;
  184. hdrstr.bm_trans    = hdrdef.bm_trans;
  185. hdrstr.bm_dmwd3    = hdrdef.bm_dmwd3;
  186. hdrstr.bm_pgwidth  = hdrdef.bm_pgwidth;
  187. hdrstr.bm_pgheight = hdrdef.bm_pgheight;
  188.  
  189.     for ( --argc ; (argc > 0) ; --argc )
  190.     {
  191.         ++argv;
  192.  
  193.         if(**argv == '-')
  194.         {
  195.             dashflag(*argv);
  196.         }
  197.         else
  198.         {
  199.  
  200.             if (inpath == STDIN)
  201.             {
  202.                 inpath = open(*argv,O_RDONLY);
  203.                 if (inpath < -3)
  204.                 {
  205.                     exit(inpath);
  206.                 }
  207.             }       /* End branch Open inpath */
  208.             else
  209.             {
  210.                 outpath = creat(*argv,0);
  211.                 errno = outpath;
  212.                 if (outpath == ERROR)
  213.                 {
  214.                     exit(errno);
  215.                 }   /* Endif output file error */
  216.             }   /* Endif Infile/Outfile openning */
  217.         }   /* Endif Dash/Not dash parameters */
  218.     }   /* Endloop Get Arguments */
  219.  
  220. /* **If multiple files implimented then loop will start here.** */
  221.  
  222.     newfflag = TRUE;
  223.  
  224.     inlen  = DGVBYTES;  /* This may be changed later if Dash switch. */
  225.     outlen = (hdrstr.bm_w * planes / 8);   /* Up to 640 bytes */
  226.     lastln = (hdrstr.bm_h - 1);            /* Usually 400 lines */
  227.  
  228.     cntr  = planes;
  229.     white = 0;
  230.     for(;;)
  231.     {
  232.         white = white << 1;
  233.         ++white;
  234.         --cntr;
  235.         if(cntr == 0)
  236.         {
  237.             break;
  238.         }
  239.     }   /* Endloop No. Colours */
  240.     nclrs = white + 1;
  241.  
  242. /* Write IFF Header: */
  243.  
  244.     iflnbmhd = 20;             /* sizeof(hdrstr) */
  245.     iflncmap = (nclrs * 3);            /* 3 bytes per colour */
  246.     iflnbody = (long) (hdrstr.bm_h * outlen); /* (lns * bytes) */
  247.  
  248.     /* (3 headers) * (8 bytes ea.) = 24 */
  249.     iflnilbm = (unsigned) (24 + iflnbmhd + iflncmap + iflnbody);
  250.  
  251.     iflnform = (unsigned) iflnilbm + 4;
  252.  
  253.     write(outpath,ifhform,4);
  254.     write(outpath,&iflnform,4);
  255.  
  256.     write(outpath,ifhilbm,4);
  257. /*    write(outpath,&iflnilbm,4);  IRREGULAR Chunk header -- No Length! */
  258.  
  259.     write(outpath,ifhbmhd,4);
  260.     write(outpath,&iflnbmhd,4);
  261.  
  262.     strpntr = (char *)(&hdrstr.bm_w); /* This style should work for all C */
  263.     write(outpath,strpntr,20); /* Write Structure */
  264.  
  265.     write(outpath,ifhcmap,4);
  266.     write(outpath,&iflncmap,4);
  267.  
  268.     lncntr   = 0;         /* No particular reason to use 'lncntr' */
  269.     cbyte[0] = 0;
  270.     for(;;)
  271.     {
  272.         if(lncntr == nclrs)
  273.         {
  274.             break;
  275.         }
  276.         cntr = 0;
  277.         for(;;)
  278.         {
  279.             if(cntr == 3)
  280.             {
  281.                 break;
  282.             }
  283.             write(outpath,cbyte,1);
  284.             ++cntr;
  285.         }   /* Endloop write 3 times */
  286.  
  287.         ++lncntr;
  288.         cbyte[0] = lncntr * 255 / (nclrs - 1);
  289.  
  290.     }   /* Endloop write IFF Color Map */
  291.  
  292.     write(outpath,ifhbody,4);
  293.     write(outpath,&iflnbody,4);
  294.  
  295. /* Convert Digiview File and Store */
  296.     lncntr = 0;
  297.     fdone = FALSE;
  298.     for(; (fdone == FALSE) ;)
  299.     {
  300.  
  301. /* Get an Input Line */
  302.         offset  = 0;
  303.         report=read(inpath,inbuf,inlen);
  304.  
  305.         if (report != inlen)
  306.         {
  307.             fprintf(stderr,"DGVCON: File short\n");
  308.         }
  309.  
  310. /* Convert The Line */
  311.  
  312.         toiff();
  313.  
  314.         write(outpath,outbuf,outlen);      /* Store the line. */
  315.         ++lncntr;
  316.  
  317.         if (lncntr > lastln)
  318.         {
  319.             fdone = TRUE;
  320.         }
  321.     }
  322.     exit(0);
  323.  
  324. } /* End of main() */
  325.  
  326. /* -------------------------------------------------------- */
  327.  
  328. dashflag(param)
  329.  
  330. char    *param;
  331.  
  332. {
  333.     extern int  bright;
  334.     extern int  contrast;
  335.     extern int  planes;
  336.     extern int  verbose;
  337.  
  338.     for (; (*param != '\0') ; ++param)
  339.     {
  340.         switch ((int)*param)
  341.         {
  342.             case '?':
  343.                 shorthelp();
  344.                 exit(0);
  345.             case 'b':
  346.                 ++param;
  347.                 if (*param == '=')
  348.                 {
  349.                     ++param;
  350.                 }
  351.                 bright = atoi(param);
  352.                 continue;
  353.             case 'c':
  354.                 ++param;
  355.                 if (*param == '=')
  356.                 {
  357.                     ++param;
  358.                 }
  359.                 contrast = atoi(param);
  360.                 continue;
  361.             case 'p':
  362.                 ++param;
  363.                 if (*param == '=')
  364.                 {
  365.                     ++param;
  366.                 }
  367.                 planes = atoi(param);
  368.                 continue;
  369.             case 't':
  370.                 ++param;
  371.                 if (*param == '=')
  372.                 {
  373.                     ++param;
  374.                 }
  375.                 if(atoi(param) == 1)
  376.                 {
  377.                     hdrstr.bm_w = 320;
  378.                     hdrstr.bm_h = 200;
  379.                 }
  380.                 continue;
  381.             case 'v':
  382.                 verbose = TRUE;
  383.                 continue;
  384.             default:
  385.                 continue;
  386.         }   /* Endswitch */
  387.     }   /* Endloop */
  388. }   /* End of dashflag() */
  389.  
  390. /* -------------------------------------------- */
  391.  
  392. shorthelp()
  393. {
  394.     printf("DgvCon Converts Digiview IP uncompressed file to ");
  395.     printf("IFF/ILBM formats.\n\n");
  396.     printf("dgvcon [-?v] [-b=nn] [-c=nn] [-p=n] [-t=n] ");
  397.     printf("[infile] [outfile]\n\n");
  398.     printf("Dash Flags:\n");
  399.     printf(" ?   Short Help\n");
  400.     printf(" b   Bright +/-255\n");
  401.     printf(" c   Contrast %%\n");
  402.     printf(" p   IFF Bit Planes (1-8)\n");
  403.     printf(" t   type (1 = 320*200, 2 = 640*400, default = 2)\n");
  404.     printf(" v   Verbose.\n");
  405.  
  406. }  /* End of shorthelp() */
  407.  
  408. /* End of DgvCon.c */
  409.